package com.king.designpatterns.hybriddesignpattern.hybriddesignpattern.chainofcommandandresponsibility;

/**
 * CommandName抽象类：是所有命令的父类，是责任链模式的handler类，用于负责命令链表控制。
 *
 * @author king
 * @date 2021-06-11 14:44
 */
public abstract class CommandName {

    private CommandName nextOperator;
    public final String handleMessage(CommandVO vo){
        String result = "";
        // 判断是否是自己处理的参数
        if (vo.getParam().size() == 0 || vo.getParam().contains(this.getOperateParam())) {
            result = this.echo(vo);
        } else {
            if(this.nextOperator != null) {
                result = this.nextOperator.handleMessage(vo);
            }else{
                result = "命令无法执行";
            }
        }
        return result;
    }

    /**
     * 设置剩余参数谁来处理
     */
    public void setNext(CommandName _operator){
        this.nextOperator = _operator;
    }

    /**
     * 每个处理者都要处理一个后缀参数
     */
    protected abstract String getOperateParam();

    /**
     * 每个处理者都必须实现处理任务
     */
    protected abstract String echo(CommandVO vo);
}
